import { RigidBody, quat, vec3, euler, RapierRigidBody, RigidBodyProps, } from '@react-three/rapier'
import { useFrame } from '@react-three/fiber'
import { useKeyboardControls, MapControls, Gltf } from '@react-three/drei'
import * as THREE from 'three'
import * as React from 'react'

const source = '/assets/su50.glb'




let position = vec3();
const Player = (props: RigidBodyProps) => {
    const body = React.useRef<RapierRigidBody>(null)
    const cameraRef = React.useRef<any>(null)
    const [, get] = useKeyboardControls()
    const frontVector = vec3()
    const sideVector = vec3()
    const smoothedCameraTarget = vec3()

    useFrame((state, delta) => {
        if (body.current) {
            const { forward, backward, leftward, rightward } = get()
            const impulse = vec3();
            const torque = vec3();

            const cameraTarget = vec3();
            const cameraPosition = vec3();

            const impulseStrength = 40
            const torqueStrength = 3

            position.copy(vec3(body.current.translation()));
            const quaternion = quat(body.current.rotation());
            const eulerRot = euler().setFromQuaternion(
                quat(body.current.rotation())
            );

            frontVector.set(0, 0, 1).applyQuaternion(quaternion);
            sideVector.set(0, 0, -1).applyQuaternion(quaternion);

            let moving = 0;


            if (forward) {
                impulse.add(frontVector.multiplyScalar(impulseStrength))
                moving = 1;
            }

            if (backward) {
                impulse.add(sideVector.multiplyScalar(impulseStrength))
                moving = 1;
            }

            if (leftward && forward) {
                torque.y += torqueStrength;

            }

            if (rightward && forward) {
                torque.y -= torqueStrength;

            }
            if (rightward && backward) {
                torque.y += torqueStrength;

            }
            if (leftward && backward) {
                torque.y -= torqueStrength;

            }


            if (moving) {
                cameraRef.current.minDistance = cameraRef.current.getDistance()
                cameraRef.current.maxDistance = cameraRef.current.getDistance()
                cameraTarget.copy(position);
                cameraPosition.copy(cameraRef.current.target)
                smoothedCameraTarget.lerpVectors(cameraPosition, cameraTarget, delta * 5);
                cameraRef.current.target.copy(smoothedCameraTarget)

            }
            else {
                cameraRef.current.minDistance = 10
                cameraRef.current.maxDistance = 50
            }

            body.current.setLinvel(impulse, true)
            body.current.setAngvel(torque, true)
        }

    },)
    return (
        <>
            <RigidBody
                {...props}
                ref={body}
                enabledRotations={[false, true, false]}
                enabledTranslations={[true, false, true]}
            >
                <mesh >
                    <Gltf castShadow={true} receiveShadow={true} src={source} />
                </mesh>
            </RigidBody>
            <MapControls
                ref={cameraRef}
                minAzimuthAngle={Math.PI / 4}
                maxAzimuthAngle={Math.PI / 4}
                minPolarAngle={Math.PI / 4}
                maxPolarAngle={Math.PI / 4}
                enableRotate={false}
                mouseButtons={{
                    LEFT: THREE.MOUSE.PAN,
                    MIDDLE: THREE.MOUSE.PAN,
                    RIGHT: THREE.MOUSE.PAN,
                }}

            />
        </>

    );
}

export { Player, position }
